<?php

class VideoInfoService extends AbstractService
{

    /**
     * @var VideoInfoService
     */
    private static $instance;

    /**
     * Get the instance of VideoInfoService.
     *
     * @return VideoInfoService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new VideoInfoService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct()
    {
        parent::__construct();
    }

    /**
     * 处理创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId)
    {

    }

    /**
     * 处理用户登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId)
    {
        /*if (GameService::get()->isDailyFirstLogin($userId)) {
            UserService::get()->updateProfileInfo($userId, GameConst::USER_INFO_TYPE_DAILY_SHARE_TIMES, []);
        }
        $this->dailyClear($userId, GameService::get()->getReqTime());*/
    }

    /**
     * @param int $userId
     * @return array|mixed
     * @throws SzException
     */
    public function info($userId) {
        $videoInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_VIDEO_INFO);
        return $videoInfoList->toArray();
    }

    /**
     * @param $userId
     * @param $videoId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function reward($userId,$videoId,$reqTime){
        $videoSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_VIDEO, $videoId, true);
        if ($videoSetting['sp_group'] != 12) {
            return array(
                'code' => GameConst::CODE_9998
            );
        }
        /**
         * @var $videoInfoList VideoInfoVoList
         * @var $videoInfo VideoInfoVo
         */
        $videoInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_VIDEO_INFO);
        $videoInfo = $videoInfoList->getElement($videoId,false);
        if (!$videoInfo ) {
            $videoInfo = new VideoInfoVo($userId,$videoId,0,0,true);
        }
        $lastClaimTime = $videoInfo->getLastClaimTime();
        $claimCount = $videoInfo->getClaimCount();

        //获取玩家平台数据
        $platformId = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);

        if (date('Ymd',$reqTime) == date('Ymd',$lastClaimTime)) {
            //zhangpeng  20200523  今日头条特殊处理
            if ($platformId == 'tt'){
                $orderVideoCd = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'TikTokOrderVideoCd', true);
                $orderVideoCd = $orderVideoCd['value'];
                if (($reqTime - $lastClaimTime) < $orderVideoCd) {
                    return array(
                        'code' => GameConst::CODE_1050
                    );
                }
            }else{
                $orderVideoCd = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'orderVideoCd', true);
                $orderVideoCd = $orderVideoCd['value'];
                if (($reqTime - $lastClaimTime) < $orderVideoCd) {
                    return array(
                        'code' => GameConst::CODE_1050
                    );
                }
            }
        } else {
            $claimCount = 0;
        }

        if($claimCount>=$videoSetting['sp_limit']) {
            return array(
                'code' => GameConst::CODE_1051
            );
        }

        $orderReward = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'orderReward', true);
        $reward = json_decode($orderReward['value']);
        $count = isset($reward[$claimCount]) ? $reward[$claimCount] : 1; // 配置不一致补发1钻石
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_MONEY, $count, true, true);
        $videoInfo->setClaimCount(++$claimCount);
        $videoInfo->setLastClaimTime($reqTime);

        if ($videoInfo->isInsert() && !SzUtility::checkArrayKey($videoId, $videoInfoList->getList())) {
            $videoInfoList->addElement($videoInfo);
            $videoInfoList->setElement($videoId,$videoInfo);
        } else {
            $videoInfoList->updateElement($videoId,$videoInfo);
        }

        SzPersister::get()->setVoList($videoInfoList);

        NewTaskService::get()->updateProgress($userId, GameConst::TASK_WATCH_VIDEO, 1);
        NewTaskService::get()->updateDailyTask($userId,GameConst::DAILY_TASK_VIDEO,1);

        return array(
            'code' => GameConst::CODE_1000,
            'videoInfo' => $videoInfo->toArray()
        );

    }
}
